/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.lista05;


import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;
import util.HibernateUtil;
/**
 *
 * @author Mendel
 */
public class PostgreDAO extends DAOFactory{
    public Session session = null;
    
    public PostgreDAO(){
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }
    public Session getSession(){
        return this.session;
    }
    public boolean cadastrarTarefa(int opcaoTipoTarefa, int opcaoTarefa) throws LotadoException, ImportanciaException {
        DAOFactory dao = DAOFactory.getDAOFactory(POSTGREE);
        org.hibernate.Transaction tx = null;
        Tarefa tarefa = null;
        
        if (opcaoTipoTarefa == 1 || opcaoTipoTarefa == 2) 
        {
            if (opcaoTarefa == 1) 
            {
                int wCodigo = 0;
                String wDescricao = "0";
                String wData = "0";
                int wDuracao = 0;
                String wLocal = "0";
                if(opcaoTipoTarefa == 1)
                {
                    tarefa = new TarefaGerencial(wCodigo, wDescricao, wData, wDuracao, wLocal);
                    
                }
                else
                {
                    tarefa = new TarefaDesenvolvimento(wCodigo, wDescricao, wData, wDuracao, wLocal);
                    
                }  
            }
            else if (opcaoTarefa == 2) 
            {
                int wCodigo = 0;
                String wDescricao = "";
                String wData = "";
                int wDuracao = 0;
                String wLocal = "";
                int wImportancia = 0;
                while (true)
                {
                    wImportancia = 0;
                    if (wImportancia < 1 || wImportancia > 5) 
                    {
                        throw new ImportanciaException();
                    }
                    else 
                    {
                        break;
                    }
                }
                int wUrgencia = 0;
                while (true) 
                {
                    wUrgencia = 0;
                    if (wUrgencia < 1 || wUrgencia > 5) 
                    {
                        throw new ImportanciaException();
                    }
                    else 
                    {
                        break;
                    }
                }
                double wValorHora = 0;
                if (opcaoTipoTarefa == 1) 
                {
                    tarefa = new TarefaGerencial(wCodigo, wDescricao, wData, wDuracao, wLocal, wImportancia, wUrgencia, wValorHora);                    
                } 
                else 
                {
                    tarefa = new TarefaDesenvolvimento(wCodigo, wDescricao, wData, wDuracao, wLocal, wImportancia, wUrgencia, wValorHora);                    
                }
            }      
        }else{
          if (opcaoTarefa == 1) 
            {
                String wDescricao = "";
                String wData = "";
                int wDuracao = 0;
                String wLocal = "";
                tarefa = new TarefaDesenvolvimentoAuto(wDescricao, wData, wDuracao, wLocal);
            }
            else if (opcaoTarefa == 2) 
            {
                String wDescricao = "";
                String wData = "";
                int wDuracao = 0;
                String wLocal = "";
                int wImportancia = 0;
                while (true) 
                {
                    wImportancia = 0;
                    if (wImportancia < 1 || wImportancia > 5) 
                    {
                        throw new ImportanciaException();
                    } 
                    else
                    {
                        break;
                    }
                }
                int wUrgencia = 0;
                while (true) 
                {
                    wUrgencia = 0;
                    if (wUrgencia < 1 || wUrgencia > 5) 
                    {
                        throw new ImportanciaException();
                    } 
                    else 
                    {
                        break;
                    }
                }
                double wValorHora = 0;
                tarefa = new TarefaDesenvolvimentoAuto(wDescricao, wData, wDuracao, wLocal, wImportancia, wUrgencia, wValorHora);                
            }
        }
        Serializable serial = null;
        try {
            tx = dao.getSession().beginTransaction();
            serial = dao.getSession().save(tarefa);
            tx.commit();
        }catch(Exception e){
            
        }
        if(serial != null)
            return true;
        else
            return false;
    }

    @Override
    public boolean removerTarefa(int codTarefa) throws SemTarefaException {
        DAOFactory dao = DAOFactory.getDAOFactory(POSTGREE);
        org.hibernate.Transaction tx = null;
        String hql = "delete from tarefa where codigo = '"+codTarefa+"'";
        int row = 0;
        try {
            tx = dao.getSession().beginTransaction();
            row = dao.getSession().createQuery(hql).executeUpdate();
            
        }catch(Exception e){
            
        }
        if (row == 0){
            return false;
        }
        else{
            return true;
        }
    }

    @Override
    public TarefaTO consultarTarefa(int codigo) throws SemTarefaException {
        DAOFactory dao = DAOFactory.getDAOFactory(POSTGREE);
        org.hibernate.Transaction tx = null;
        Tarefa tarefa = null;
        TarefaTO tarefaAux = null;
        try {
            tx = dao.getSession().beginTransaction();  
            tarefa = (Tarefa) dao.getSession().createQuery("select * from tarefa where codigo = '"+codigo+"'").uniqueResult();            
        } catch (Exception e) {
            tx.rollback();
        }finally{           
            tarefaAux = new TarefaTO(tarefa.getCodigo(),
                                     tarefa.getDescricao(),
                                     tarefa.getData(),
                                     tarefa.getDuracao(),
                                     tarefa.getLocal());
            return tarefaAux;
        }
    }

    @Override
    public TarefaTO[] listarTarefa() {
        DAOFactory dao = DAOFactory.getDAOFactory(POSTGREE);
        
        List<Tarefa> lista = null;
        TarefaTO[] arrayAux = null;
        try {
            org.hibernate.Transaction tx = dao.getSession().beginTransaction();  
            lista = dao.getSession().createQuery("select * from tarefa").list();
        } catch (Exception e) {
            
        }finally{
            //escrever código para passar todos os elementos da lista para objetos TarefaTO.
            int i = 0;
            for(int y = 0; y < lista.size(); y++)
            {
                
                arrayAux[i] = new TarefaTO(lista.get(y).getCodigo(),
                                           lista.get(y).getDescricao(),
                                           lista.get(y).getData(),
                                           lista.get(y).getDuracao(),
                                           lista.get(y).getLocal());
                i++;              
            }
            return arrayAux;           
        }
                    
    }

    @Override
    public boolean iniciarTarefa(int codigo) throws SemTarefaException {
        DAOFactory dao = DAOFactory.getDAOFactory(POSTGREE);
        org.hibernate.Transaction tx = null;
        Tarefa tarefa = null;
        try {
            tx = dao.getSession().beginTransaction();  
            tarefa = (Tarefa) dao.getSession().createQuery("select * from tarefa where codigo = '"+codigo+"'").uniqueResult();              
        } catch (Exception e) {
            tx.rollback();
        }
        if (tarefa.getCodigo() == codigo) 
        {
            tarefa.iniciarTarefa();
            return true;
        }else
            return false;
    }

    @Override
    public boolean encerrarTarefa(int codigo, double wValorCobrado) throws SemTarefaException {
        DAOFactory dao = DAOFactory.getDAOFactory(POSTGREE);
        org.hibernate.Transaction tx = null;
        Tarefa tarefa = null;
        try {
            tx = dao.getSession().beginTransaction();  
            tarefa = (Tarefa) dao.getSession().createQuery("select * from tarefa where codigo = '"+codigo+"'").uniqueResult();              
        } catch (Exception e) {
            tx.rollback();
        }
        if (tarefa.getCodigo() == codigo) 
        {
            tarefa.encerrarTarefa(wValorCobrado);
            return true;
        }else
            return false;
    }

    @Override
    public boolean alterarImportancia(int codigo, int wOpcao) throws SemTarefaException {
        DAOFactory dao = DAOFactory.getDAOFactory(POSTGREE);
        org.hibernate.Transaction tx = null;
        Tarefa tarefa = null;
        try {
            tx = dao.getSession().beginTransaction();  
            tarefa = (Tarefa) dao.getSession().createQuery("select * from tarefa where codigo = '"+codigo+"'").uniqueResult();              
        } catch (Exception e) {
            tx.rollback();
        }
        if (wOpcao == 1) 
        {
            tarefa.incrementaImportancia();
            return true;
        }
        else if (wOpcao == 2)
        {
            tarefa.decrementaImportancia();
            return true;
        }else
            return false;
    }

    @Override
    public boolean cancelarTarefa(int codigo) throws SemTarefaException {
        DAOFactory dao = DAOFactory.getDAOFactory(POSTGREE);
        org.hibernate.Transaction tx = null;
        Tarefa tarefa = null;
        try {
            tx = dao.getSession().beginTransaction();  
            tarefa = (Tarefa) dao.getSession().createQuery("select * from tarefa where codigo = '"+codigo+"'").uniqueResult();              
        } catch (Exception e) {
            tx.rollback();
        }
        if (tarefa.getCodigo() == codigo) 
        {
            tarefa.cancelarTarefa();
            return true;
        }else
            return false;
    }
    
}
